package com.csgc.camera.biz.other.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager

import com.csgc.camera.base.LazyFragment
import com.csgc.camera.bean.MaskItemInfo
import com.csgc.camera.biz.other.adapter.MaskTypeAdapter

import com.csgc.camera.events.ChangeMaskEvent
import com.csgc.camera.events.RewardTempVipEvents
import com.csgc.camera.events.TempVipExpireEvents
import com.csgc.camera.storage.MaskPreference
import com.csgc.camera.widget.mask.MaskKaoQin
import com.cysd.spsyxj.databinding.FragmentMaskPageBinding

import com.drake.channel.receiveEvent
import com.drake.channel.sendEvent
import com.elvishew.xlog.XLog

class MaskPageFragment: LazyFragment<FragmentMaskPageBinding>() {

    companion object{
        fun getInstance(list:ArrayList<MaskItemInfo>):MaskPageFragment{

            val fragment = MaskPageFragment()
            val bundle = Bundle()
            bundle.putParcelableArrayList("list",list)
            fragment.arguments = bundle
            return fragment
        }
    }
    private val adapter: MaskTypeAdapter by lazy {
        val adapter = MaskTypeAdapter(requireContext())
        adapter.listener = object :MaskTypeAdapter.OnMaskClickListener{
            override fun onMaskClick(type: Int, id: Int) {
                //更新模版
                sendEvent(ChangeMaskEvent(type, id))
            }
        }
        adapter
    }

    override fun bindingInflater(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): FragmentMaskPageBinding {
        return FragmentMaskPageBinding.inflate(inflater,container,false)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val manager = GridLayoutManager(requireContext(),2)
        manager.orientation = GridLayoutManager.VERTICAL
        binding.rvMaskList.layoutManager = manager
        val list:ArrayList<MaskItemInfo> =  arguments?.getParcelableArrayList("list")?: ArrayList()

        adapter.list.clear()
        adapter.list.addAll(list)
        adapter.currentMaskType = MaskPreference.instance.getMaskType()
        adapter.currentMaskId = MaskPreference.instance.getMaskId()

        adapter.notifyDataSetChanged()
        binding.rvMaskList.adapter = adapter



        receiveEvent<ChangeMaskEvent> {
            adapter.currentMaskType = it.type
            adapter.currentMaskId = it.id
            adapter.notifyDataSetChanged()

        }

    }

    override fun lazyInit() {
        //获取到临时会员
        receiveEvent<RewardTempVipEvents> {
            adapter.notifyDataSetChanged()
        }
        //临时会员到期
        receiveEvent<TempVipExpireEvents> {
            adapter.notifyDataSetChanged()
        }

    }
}